﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogic;
using BusinessObject;
namespace WebTeamplate
{
    public partial class Admin_usercontrol_LeftMenu : BaseUserControl
    {
        private List<MenuResult> lstMenu = new List<MenuResult>();
        public string menuHTML = string.Empty;
        public string currentURL = string.Empty;
        public string currentFunction = string.Empty;
        public List<MenuResult> MenuList
        {
            get
            {
                return lstMenu;
            }
            set
            {
                lstMenu = value;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            CreateMenu();

            Literal ltr=(Literal)this.Page.Master.FindControl("ltrFunctionName");
            if (ltr != null)
            {
                ltr.Text = currentFunction;
            }

        }
        bool isFirst = true;
        private string GetLinkForParent(MenuResult objParent, bool select, bool lastNode)
        {
            string display = string.Empty;
            if (select)
                display = "";
            else display = "none";
            string result=string.Empty;
            if(!isFirst)
                result+=@"</ul></div>";
            isFirst = false;
            result += @"
                        <div class='portlet-header ui-widget-header'>" + objParent.Name + @"
                            <span class='ui-icon ui-icon-circle-arrow-s' id='sp_" + objParent.Id + "' onclick='ToggeMenu(" + objParent.Id + @");'></span></div>";
            result+=@"<div class='portlet-content' id='mn_" + objParent.Id + "' style='display: " + display + @";'>
                <ul class='side-menu'>";
            return result;
        }
        private void CreateMenu()
        {
            MenuResult objParent = new MenuResult();           
            string li=string.Empty;
            string liClass = string.Empty;
            bool reset = false;
            bool select = false;
            while (lstMenu.Count > 0)
            {
                MenuResult objCurrent = lstMenu[0];
                if (objCurrent != null)
                {
                    if (objCurrent.ParentID <= 0)//parent
                    {
                        objParent = objCurrent;
                    }
                    else
                    {

                        int NextParent = int.MinValue;
                        if(lstMenu.Count>1)
                            NextParent=lstMenu[1].ParentID;
                        if (NextParent!=objCurrent.ParentID)
                        {
                            reset = true;                       
                        }
                        if (objCurrent.Link.ToLower().Trim() == currentURL.ToLower().Trim())
                        {
                            liClass = "set_themeactive";
                            select = true;
                            currentFunction = objCurrent.Name;
                        }
                        else
                        {
                            liClass = "";
                           // select = false;
                        }
                        //liClass = "set_theme";
                        li += @"<li><a class='" + liClass + @"'  href='" + objCurrent.Link + "' title='" + objCurrent.Name + "'>" + objCurrent.Name + "</a></li>";
                    }
                }
                if (reset)
                {
                    menuHTML += GetLinkForParent(objParent, select, select) + li;
                    li = string.Empty;
                    reset = false;
                    select = false;
                }
                lstMenu.Remove(objCurrent);
            }
            menuHTML += "</ul></div>";
          //  Response.Write(menuHTML);
        }
    }
}
